<?php


namespace app\admin\controller;


use app\admin\validate\RoutesValidate;
use app\BaseController;
use app\Code;
use app\lib\exception\ServiceException;
use app\model\Routes;
use app\service\AuthService;
use app\service\RoutesService;
use think\App;
use think\exception\ValidateException;

class RoutesController extends BaseController
{
    private $service;

    public function __construct(App $app)
    {
        $this->service = new RoutesService();
        parent::__construct($app);
    }

    public function addRoute(){
        $param = $this->request->param(['url', 'method']);
        $roles = $this->request->param('roles', '');

        validate(RoutesValidate::class)
            ->scene('add')
            ->check($param);

        $result = $this->service->addRoute($param['url'], $param['method'], $roles);

        return $this->returnJson($result);

    }

    public function editRoute(Routes $routes){
        $roles = $this->request->param('roles', '');
        $url = $this->request->param('url', '');

        $this->service->editRoute($routes->id, $url, $roles);

        return $this->returnJson();
    }

    public function getRoute(){
        $keyword = $this->request->param('keyword', '');
        $page = $this->request->param('page', 1);
        $limit = $this->request->param('limit', 20);

        $result = $this->service->getRoute($keyword, $page, $limit);

        return $this->returnJson($result);
    }

    public function getById($id){
        $result = $this->service->find($id);
        return $this->returnJson($result);
    }

    public function delRoute($id){
        $this->service->delRoute($id);

        return $this->returnJson();
    }
}